<?php
namespace Sys\Controller;
use THINK\Controller;
class LoginController extends Controller{
	private $pwd = null;
	public function __construct(){
		parent::__construct();
	}

	public function getPwd(){
		$this ->pwd = rand(11111,99999);
		setcookie("pwd",$this ->pwd);
		$getEmail = I('post.email');
		Vendor('phpmailer.PHPMailerAutoload');
	
	$mail = new \PHPMailer;
	//https://
	//邮件服务器信息配置
	$mail -> ISSMTP();  //设置邮件发送协议 smtp|pop3|imap
	$mail -> CharSet = "utf-8";  //设置邮件编码
	$mail->SMTPSecure = "ssl";  // SMTP 安全协议
	$mail -> Port = 465;          // 邮件端口
	//$mail -> Host = "smtp.163.com"; // 使用的邮件服务器
	$mail -> Host = "smtp.qq.com"; // 使用的邮件服务器

	$mail -> SMTPAuth = true;  //设置phpmail发送邮件是否需要验(username&password)
	if($mail -> SMTPAuth){
		$mail -> Username = "1341570291@qq.com";
		$mail -> Password = 'gtnkspbltdppjeig';//密码
	}
	$mail -> From = "1341570291@qq.com";    //来源from
	$mail -> IsHTML(true); // 是否发送html邮件

	//$mail->AddAttachment('./email.txt','email.txt'); // 添加附件,并指定名称

	//发送邮件信息
	$mail -> Addaddress($getEmail); // 收件人
	//dump($getEmail);
	$mail->FromName = 'laowang2';//发送人姓名
	$mail -> Subject = '验证码';   // 标题
	$mail -> Body = '<b>'.$this->pwd.'</b>';    // 内容
	if($mail->send()){
		echo 'ok';
	}else{
		echo $mail->ErrorInfo;
	}
	}

	public function login(){
		$this->display();
	}
	public function doLogin(){
		//dump($_POST);
		//$title=I('post.title');
		 if(I('post.role')=='0'){
            $this->teacherLogin();
        }else{
            $this->studentLogin();
        } 
    }    
        public function teacherLogin(){
			if($_COOKIE['pwd'] != I('post.prove') || I('post.prove') == null){
			$this->error('验证码不对');
			return;
		}
        	$where = [
			'mtel' => I('post.mtel'),
			'pwd' =>md5( I('post.pwd')),
		];
		$member = M('teachers')
					-> join('__ROLES__ ON __TEACHERS__.role = __ROLES__.role_id')
					-> where($where)
					//->fetchSql(true)
					-> find();
					//dump($member);
		if($member == false){
			 $this-> error('登录失败',U('login'));
		}else{
			$arr = explode(",",$member['menu_ids']);
			session('name',$member['tname']);
			//session('title',$title);
			session('role',$arr);
			session('id',$member['teacher_id']);
			session('tp',$_POST['role']);
			$this-> success('登录成功',U('Index/index'));
		}
		
        	
	}
	public function studentLogin(){
		
		if($_COOKIE['pwd'] != I('post.prove') || I('post.prove') == null){
			$this->error('验证码不对');
			return;
		}
		$where = [
			'email'=>I('post.email'),
			'pwd' => $pwd,
			'tel' => I('post.mtel'),
			'pwd' => md5(I('post.pwd')),
		];
		$member = M('students')
					-> join('__ROLES__ ON __STUDENTS__.role = __ROLES__.role_id')
					-> where($where)
					-> find();
				//dump($member);
		if($member == false){
			  $this-> error('登录失败',U('login'));
		}else{
			$arr = explode(",",$member['menu_ids']);
			session('name',$member['sname']);
			session('tp',$_POST['role']);
			session('role',$arr);
			session('id',$member['student_id']);
			$this-> success('登录成功',U('Index/index'));
		}
	}
	public function doLogout(){
		session(null);
		$this-> success('再见',U('login'));
	}
}